#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   ListView.py
@Time    :   2020/07/15 10:05:04
@Author  :   Xiong qiufeng
@Version :   1.0
@Contact :   1528399071@qq.com
@License :   (C)Copyright 2017-2018, Liugroup-NLPR-CASIA
@Desc    :   None
'''


from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

from .DeviceItemWidget import DeviceItemWidget

class DeviceTableWidget(QTableWidget):
    select_row = 0

    def __init__(self, parent=None):
        super(QTableWidget, self).__init__(parent)
        # 点击事件
        self.clicked.connect(self.handleItemClicked)
        self.setColumnCount(6)	
        self.setHorizontalHeaderLabels(['名称', '位置', '型号', 'sn', '状态'])
        # 下面代码让表格100填满窗口
        self.horizontalHeader().setStretchLastSection(True)
        self.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        # 多选
        self.setSelectionBehavior(QAbstractItemView.SelectRows)
        # self.setSelectionMode(QAbstractItemView.ExtendedSelection)

    def handleItemClicked(self, item):
        self.select_row = item.row()

    def load(self, items):
        self.clear()
        self.setRowCount(len(items))
        self.setHorizontalHeaderLabels(['名称', '位置', '型号', 'sn', '状态'])
        # self.btn_delete_list = {}
        # self.btn_modify_list = {}
        for i in range(0,len(items)):
            item = items[i]
            label_image = None
            icon = None
            if item['model'] == 'FSC-01':
                # label_image = QLabel(self, pixmap=QPixmap(':icons/images/guangji.png'))
                icon = QIcon(':icons/images/guangji.png')
            if item['model'] == 'FSC-02':
                # label_image = QLabel(self, pixmap=QPixmap(':icons/images/men.png'))
                icon = QIcon(':icons/images/men.png')
            if item['model'] == 'FSC-03':
                # label_image = QLabel(self, pixmap=QPixmap(':icons/images/hongwai.png'))
                icon = QIcon(':icons/images/hongwai.png')
            if item['model'] == 'FSC-04':
                # label_image = QLabel(self, pixmap=QPixmap(':icons/images/fusheyuan.png'))
                icon = QIcon(':icons/images/fusheyuan.png')
            if item['model'] == 'FSC-05':
                # label_image = QLabel(self, pixmap=QPixmap(':icons/images/guidao.png'))
                icon = QIcon(':icons/images/guidao.png')
            self.setItem(i, 0, QTableWidgetItem(icon, str(item['name'])))
            # if label_image:
            #     self.setCellWidget(i, 0, label_image)
            # self.setItem(i, 1, QTableWidgetItem(str(item['name'])))
            self.setItem(i, 1, QTableWidgetItem(str(item['position'])))
            self.setItem(i, 2, QTableWidgetItem(str(item['model'])))
            self.setItem(i, 3, QTableWidgetItem(str(item['sn'])))
            if item['alive'] == 1:
                self.setItem(i, 4, QTableWidgetItem(QIcon(':icons/images/circle-green.png'), '在线'))
            if item['alive'] == 0:
                self.setItem(i, 4, QTableWidgetItem(QIcon(':icons/images/circle-red.png'), '离线'))


            # gridlayout = QGridLayout(self)
            # label_name = QLabel(item['name'])
            # label_sn = QLabel(item['sn'])

            # gridlayout.addWidget(label_name, 0,0,1,1)
            # gridlayout.addWidget(label_sn, 0,0,1,1)

            # group = QGroupBox()
            # group.setLayout(gridlayout)
            itemWidget = DeviceItemWidget()
            # self.setItem(i, 5, QTableWidgetItem(itemWidget))
            self.setCellWidget(i, 5, itemWidget)
            

